import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import jd.client.JDClient;
import jd.client.action.ActionAddLinksGrabberRequest;
import jd.client.action.ActionAddLinksGrabberResponse;
import jd.client.action.ActionStartRequest;
import jd.client.action.ActionStartResponse;
import jd.client.action.ActionUpdateRequest;
import jd.client.action.ActionUpdateResponse;
import jd.client.get.GetConfigResponse;
import jd.client.get.GetDownloadsAllListResponse;
import jd.client.get.GetDownloadsCurrentListResponse;
import jd.client.get.GetDownloadsFinishedListResponse;
import jd.client.get.JDGet;
import jd.client.impl.DefaultJDClient;

/**
 * 
 * @author Denis Migol
 * 
 */
public class JDClientExample {

	//private final JDClient jdClient = new HttpJDClient("192.168.1.3");
	private final JDClient jdClient = new DefaultJDClient("192.168.1.3");
	// private final JDClient jdClient = new JDClient();

	public void help() throws Exception {
		System.out.println(jdClient.help().getResponse());
	}

	public void getSpeed() throws Exception {
		System.out.println(jdClient.getSpeed());
	}

	public void getIP() throws Exception {
		System.out.println(jdClient.getIP());
	}

	public void getRandomIP() throws Exception {
		System.out.println(jdClient.getRandomIP());
	}

	public void getConfig() throws Exception {
		System.out.println(((GetConfigResponse) jdClient.execute(JDGet.GET_CONFIG)).getResponse());
	}

	public void getVersion() throws Exception {
		System.out.println(jdClient.getVersion());
	}

	public void getRCVersion() throws Exception {
		System.out.println(jdClient.getRCVersion());
	}

	public void getSpeedLimit() throws Exception {
		System.out.println(jdClient.getSpeedLimit());
	}

	public void getIsReconnect() throws Exception {
		System.out.println(jdClient.getIsReconnect());
	}

	public void getDownloadStatus() throws Exception {
		System.out.println(jdClient.getDownloadStatus());
	}

	public void getDownloadsCurrentCount() throws Exception {
		System.out.println(jdClient.getDownloadsCurrentCount());
	}

	public void getDownloadsCurrentList() throws Exception {
		System.out.println(((GetDownloadsCurrentListResponse) jdClient.execute(JDGet.GET_DOWNLOADS_CURRENT_LIST))
				.getResponse());
	}

	public void getDownloadsAllCount() throws Exception {
		System.out.println(jdClient.getDownloadsAllCount());
	}

	public void getDownloadsAllList() throws Exception {
		System.out.println(((GetDownloadsAllListResponse) jdClient.execute(JDGet.GET_DOWNLOADS_ALL_LIST))
				.getResponse());
	}

	public void getDownloadsFinishedCount() throws Exception {
		System.out.println(jdClient.getDownloadsFinishedCount());
	}

	public void getDownloadsFinishedList() throws Exception {
		System.out.println(((GetDownloadsFinishedListResponse) jdClient.execute(JDGet.GET_DOWNLOADS_FINISHED_LIST))
				.getResponse());
	}

	/* actions: */

	public void actionStart() throws Exception {
		System.out.println(((ActionStartResponse) jdClient.execute(new ActionStartRequest())).getResponse());
	}

	public void actionPause() throws Exception {
		System.out.println("TODO");
	}

	public void actionStop() throws Exception {
		System.out.println("TODO");
	}

	public void actionToggle() throws Exception {
		System.out.println("TODO");
	}

	public void actionUpdate() throws Exception {
		System.out.println(((ActionUpdateResponse) jdClient.execute(new ActionUpdateRequest())).getResponse());
	}

	public void actionReconnect() throws Exception {
		System.out.println("TODO");
	}

	public void actionRestart() throws Exception {
		// System.out.println(((ActionRestartResponse) jdClient.execute(new
		// ActionRestartRequest())).getResponse());
	}

	public void actionShutdown() throws Exception {
		System.out.println("TODO");
	}

	public void actionSetDownloadLimit() throws Exception {
		System.out.println("TODO");
	}

	public void actionSetDownloadMax() throws Exception {
		System.out.println("TODO");
	}

	public void actionAddLinksGrabber() throws Exception {
		final ActionAddLinksGrabberRequest request = new ActionAddLinksGrabberRequest();
		// request.addLink("http://tinyurl.com/6o73eq");//.addLink("http://tinyurl.com/4khvhn");
		request.addLink("http://rapidshare.com/files/109307638/check");
		System.out.println(request.getUrl());
		System.out.println(((ActionAddLinksGrabberResponse) jdClient.execute(request)).getResponse());
	}

	public void actionAddContainerGrabber() throws Exception {
		System.out.println("TODO");
	}

	public void actionSaveContainer() throws Exception {
		System.out.println("TODO");
	}

	public void actionSetReconnectEnabled() throws Exception {
		System.out.println("TODO");
	}

	public void actionSetPremiumEnabled() throws Exception {
		System.out.println("TODO");
	}

	public static void main(String[] args) throws Exception {
		final JDClientExample example = new JDClientExample();

		final Method[] methods = example.getClass().getDeclaredMethods();
		for (final Method method : methods) {
			if ((method.getModifiers() & Modifier.STATIC) == 0) {
				try {
					System.out.println(method.getName() + ":");
					method.invoke(example);
					System.out.println();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
}
